% *************************************************************************
% GENERATION OF HABNET v1.0 FROM MATLAB SCRIPT
% *************************************************************************

%% Create and open the system

sys = 'HabNet_v1';
new_system(sys);
open_system(sys);

%% Read technology choices from vectors

% For each of the functions that should be accomplished the technology
% choice vector is coded on 2 bits (thus allowing 4 system choices for each
% function)
% Depending on where the 1 is in the technology choice vector, the
% corresponding system will be selected out of the library

% For the pressure controlling function - a 1 in the first position
% indicates that a PPRV will be used, a 1 in the second position indicates
% a regulator will be used
pressure_controlling_tech_choice = [1 1];

% O2 storing function
% oxygen tank - 1 on the first position
O2_storing_tech_choice = [1 0];

% Atmosphere providing function
% AR on the first position, PGA on the second
Atmosphere_providing_tech_choice = [1 1];

% CO2 removing function
% CCC1 is 1 in the first position
% CCC2 is 1 in the second position
CO2_removing_tech_choice = [1 0];
%CO2_removing_tech_choice = [0 1];

% Circulating air function
% Fan is 1 in the first position
Circulating_Air_tech_choice = [1 0];

% Cooling condensing function
% Sublimator is 1 in the first position
cooling_condensing_tech_choice = [1 0];

%% Add blocks
% proper layout specification
% the position parameter provides the top left (x,y) and lower right (x+w,
% y+h) corners of the blocks
% x increases to the right and y increases down
% blocks are a standard of 30 by 30, offsets are 60

x = 30;
y = 30;
w = 30;
h = 30;

%% Pressure controlling

% insert the technology choices for the pressure controlling block

if pressure_controlling_tech_choice(1) == 1
    % need to insert a PPRV
    PPRV_inserted = 1;
    pos = [x+10*w y x+14*w y+h];
    add_block('HabNetLibrary/PPRV', [sys '/PPRV'], 'Position', pos);
    
end

if pressure_controlling_tech_choice(2) == 1
    % need to insert a regulator
    regulator_inserted = 1;
    pos = [x+10*w y+2*h x+15*w y+4*h];
    add_block('HabNetLibrary/Regulator', [sys '/Regulator'], 'Position', pos);
end

% if a regulator is inserted it needs to be initialized
if regulator_inserted == 1
    pos = [x y+2*h x+4*w y+4*h];
    add_block('HabNetLibrary/Initialize_Regulator', [sys '/Initialize_Regulator'], 'Position', pos);
    
    pos = [x+6*w y+2*h x+7*w y+3*h];
    add_block('built-in/Sum', [sys '/Sum1'], 'Position', pos);
    
    pos = [x+6*w y+5*h x+7*w y+6*h];
    add_block('built-in/Memory', [sys '/Memory1'], 'Position', pos);
    set_param('HabNet_v1/Memory1','Orientation', 'left');
end

% insert the pressure controlling block
pos = [x+10*w y+5*h x+20*w y+8*h];
add_block('HabNetLibrary/Pressure_Controlling', [sys '/Pressure_Controlling'], 'Position', pos);

%% O2 storing

if O2_storing_tech_choice(1) == 1
    % need to insert an oxygen tank
    O2_tank_inserted = 1;
    pos = [x+11*w y+12*h x+16*w y+14*h];
    add_block('HabNetLibrary/O2_Tank', [sys '/O2_Tank'], 'Position', pos);
    set_param('HabNet_v1/O2_Tank', 'Orientation', 'left');
    
end

% if an oxygen tank is inserted it needs to be initialized
if O2_tank_inserted == 1
    pos = [x y+12*h x+4*w y+14*h];
    add_block('HabNetLibrary/Initialize_O2_Tank', [sys '/Initialize_O2_Tank'], 'Position', pos);
    
    pos = [x+6*w y+15*h x+7*w y+16*h];
    add_block('built-in/Sum', [sys '/Sum2'], 'Position', pos);
    
    pos = [x+6*w y+17*h x+7*w y+18*h];
    add_block('built-in/Sum', [sys '/Sum3'], 'Position', pos);
    
    pos = [x+6*w y+12*h x+7*w y+13*h];
    add_block('built-in/Memory', [sys '/Memory2'], 'Position', pos);
    set_param('HabNet_v1/Memory2','Orientation', 'left');
end

% insert the O2 storing block
pos = [x+10*w y+15*h x+18*w y+18*h];
add_block('HabNetLibrary/O2_Storing', [sys '/O2_Storing'], 'Position', pos);

%% Habitat Vector Initialization
pos = [x y+19*h x+4*w y+21*h];
add_block('HabNetLibrary/Initialize_Habitat_Vector_O2_Storing', [sys '/Initialize_Habitat_Vector_O2_Storing'], 'Position', pos);

%% Human and Metabolizing

pos = [x+18*w y+10*h x+23*w y+12*h];
add_block('HabNetLibrary/Human', [sys '/Human'], 'Position', pos);

pos = [x+20*w y+15*h x+28*w y+18*h];
add_block('HabNetLibrary/Metabolizing', [sys '/Metabolizing'], 'Position', pos);

% Human Initialization

pos = [x+27*w y+8*h x+29*w y+10*h];
add_block('HabNetLibrary/Initialize_Human', [sys '/Initialize_Human'], 'Position', pos);

pos = [x+24*w y+10*h x+25*w y+11*h];
add_block('built-in/Sum', [sys '/Sum4'], 'Position', pos);
set_param('HabNet_v1/Sum4','Orientation', 'left');

pos = [x+24*w y+13*h x+25*w y+14*h];
add_block('built-in/Memory', [sys '/Memory3'], 'Position', pos);
set_param('HabNet_v1/Memory3','Orientation', 'up');

%% Atmosphere providing

if Atmosphere_providing_tech_choice(1) == 1
    % need to insert an AR
    AR_inserted = 1;
    pos = [x+31*w y+18*h x+35*w y+20*h];
    add_block('HabNetLibrary/AR', [sys '/AR'], 'Position', pos);
end

if Atmosphere_providing_tech_choice(2) == 1
    % need to insert a PGA
    PGA_inserted = 1;
    pos = [x+32*w y+12*h x+37*w y+14*h];
    add_block('HabNetLibrary/PGA', [sys '/PGA'], 'Position', pos);
    
end

% need to initialize the AR
if AR_inserted == 1
    pos = [x+20*w y+20*h x+22*w y+22*h];
    add_block('HabNetLibrary/Initialize_AR', [sys '/Initialize_AR'], 'Position', pos);
    
    pos = [x+20*w y+18*h x+21*w y+19*h];
    add_block('built-in/Sum', [sys '/Sum5'], 'Position', pos);
    
    pos = [x+28*w y+22*h x+29*w y+23*h];
    add_block('built-in/Memory', [sys '/Memory4'], 'Position', pos);
    set_param('HabNet_v1/Memory4','Orientation', 'left');
end

% need to initialize the PGA

if PGA_inserted == 1
    pos = [x+38*w y+12*h x+40*w y+14*h];
    add_block('HabNetLibrary/Initialize_PGA', [sys '/Initialize_PGA'], 'Position', pos);
    
    pos = [x+36*w y+10*h x+37*w y+11*h];
    add_block('built-in/Sum', [sys '/Sum6'], 'Position', pos);
    set_param('HabNet_v1/Sum6','Orientation', 'left');
    
    pos = [x+38*w y+10*h x+39*w y+11*h];
    add_block('built-in/Memory', [sys '/Memory5'], 'Position', pos);
    set_param('HabNet_v1/Memory5','Orientation', 'left');
end

pos = [x+30*w y+5*h x+43*w y+9*h];
add_block('HabNetLibrary/Atmosphere_Providing', [sys '/Atmosphere_Providing'], 'Position', pos);

%% CO2 removing

CCC1_inserted = 0;
CCC2_inserted = 0;

if CO2_removing_tech_choice(1) == 1
    % need to insert CCC1
    CCC1_inserted = 1;
    pos = [x+50*w y+12*h x+54*w y+14*h];
    add_block('HabNetLibrary/CCC1', [sys '/CCC'], 'Position', pos);
    
end

if CCC1_inserted == 1
    pos = [x+48*w y+15*h x+52*w y+17*h];
    add_block('HabNetLibrary/Initialize_CCC1', [sys '/Initialize_CCC'], 'Position', pos);
    
    pos = [x+46*w y+12*h x+47*w y+13*h];
    add_block('built-in/Memory', [sys '/Memory6'], 'Position', pos);
    set_param('HabNet_v1/Memory6','Orientation', 'left');
    
    pos = [x+44*w y+12*h x+45*w y+13*h];
    add_block('built-in/Sum', [sys '/Sum7'], 'Position', pos);
    set_param('HabNet_v1/Sum7','Orientation', 'left');
end

% if CCC2 is to be inserted
if CO2_removing_tech_choice(2) == 1
    % need to insert a CCC2
    CCC2_inserted = 1;
    pos = [x+50*w y+12*h x+54*w y+14*h];
    add_block('HabNetLibrary/CCC2', [sys '/CCC'], 'Position', pos);
    
end

if CCC2_inserted == 1
    pos = [x+48*w y+15*h x+52*w y+17*h];
    add_block('HabNetLibrary/Initialize_CCC2', [sys '/Initialize_CCC'], 'Position', pos);
    
    pos = [x+46*w y+12*h x+47*w y+13*h];
    add_block('built-in/Memory', [sys '/Memory6'], 'Position', pos);
    set_param('HabNet_v1/Memory6','Orientation', 'left');
    
    pos = [x+44*w y+12*h x+45*w y+13*h];
    add_block('built-in/Sum', [sys '/Sum7'], 'Position', pos);
    set_param('HabNet_v1/Sum7','Orientation', 'left');
end

pos = [x+46*w y+8*h x+52*w y+10*h];
add_block('HabNetLibrary/CO2_Removing', [sys '/CO2_Removing'], 'Position', pos);

%% Circulating Air

if Circulating_Air_tech_choice(1) == 1
    % need to insert a PGA
    Fan_inserted = 1;
    pos = [x+56*w y+14*h x+60*w y+16*h];
    add_block('HabNetLibrary/Fan', [sys '/Fan'], 'Position', pos);
    
end

pos = [x+56*w y+8*h x+64*w y+10*h];
add_block('HabNetLibrary/Circulating_Air', [sys '/Circulating_Air'], 'Position', pos);

pos = [x+46*w y+4*h x+47*w y+5*h];
add_block('built-in/Memory', [sys '/Memory7'], 'Position', pos);
set_param('HabNet_v1/Memory7','Orientation', 'left');

pos = [x+32*w y+3*h x+33*w y+4*h];
add_block('built-in/Sum', [sys '/Sum8'], 'Position', pos);
set_param('HabNet_v1/Sum8','Orientation', 'left');

pos = [x+46*w y+5.5*h x+52*w y+6.5*h];
add_block('HabNetLibrary/Initialize_Habitat_Vector_Circulating_Air', [sys '/Initialize_Habitat_Vector_Circulating_Air'], 'Position', pos);

%% Cooling condensing

if cooling_condensing_tech_choice(1) == 1
    % need to insert a sublimator
    Sublimator_inserted = 1;
    pos = [x+65*w y+14*h x+70*w y+16*h];
    add_block('HabNetLibrary/Sublimator', [sys '/Sublimator'], 'Position', pos);
end

pos = [x+68*w y+8*h x+74*w y+10*h];
add_block('HabNetLibrary/Cooling_Condensing', [sys '/Cooling_Condensing'], 'Position', pos);

pos = [x+46*w y+2.5*h x+47*w y+3.5*h];
add_block('built-in/Memory', [sys '/Memory8'], 'Position', pos);
set_param('HabNet_v1/Memory8','Orientation', 'left');

pos = [x+32*w y+h x+33*w y+2*h];
add_block('built-in/Sum', [sys '/Sum9'], 'Position', pos);
set_param('HabNet_v1/Sum9','Orientation', 'left');

pos = [x+46*w y x+54*w y+1.5*h];
add_block('HabNetLibrary/Initialize_Habitat_Vector_Cooling_Condensing', [sys '/Initialize_Habitat_Vector_Cooling_Condensing'], 'Position', pos);

%% Insert to workspace blocks so that the AR, tank and CCC1 states can be plotted


pos = [x+44*w y+18*h x+50*w y+19*h];
add_block('built-in/To Workspace', [sys '/ToWorkspace1'], 'Position', pos);
set_param('HabNet_v1/ToWorkspace1', 'VariableName','Hab_Vector_In_before_CCC','SaveFormat','Structure', 'MaxDataPoints', 'inf');

pos = [x+10*w y+9*h x+14*w y+10*h];
add_block('built-in/To Workspace', [sys '/ToWorkspace2'], 'Position', pos);
set_param('HabNet_v1/ToWorkspace2', 'VariableName','Tank_Output','SaveFormat','Structure', 'MaxDataPoints', 'inf');

pos = [x+53*w y+18*h x+57*w y+19*h];
add_block('built-in/To Workspace', [sys '/ToWorkspace3'], 'Position', pos);
set_param('HabNet_v1/ToWorkspace3', 'VariableName','CCC','SaveFormat','Structure', 'MaxDataPoints', 'inf');

%% Insert to workspace blocks for mass, power and volume calculations

pos = [x y+30*h x+6*w y+31*h];
add_block('built-in/To Workspace', [sys '/ToWorkspace4'], 'Position', pos);
set_param('HabNet_v1/ToWorkspace4', 'VariableName','params_O2_Storing_Tech_Choice','SaveFormat','Structure','MaxDataPoints', 'inf');

pos = [x+7*w y+30*h x+15*w y+31*h];
add_block('built-in/To Workspace', [sys '/ToWorkspace5'], 'Position', pos);
set_param('HabNet_v1/ToWorkspace5', 'VariableName','params_Pressure_Controlling_Tech_Choice2','SaveFormat','Structure','MaxDataPoints', 'inf');

pos = [x+16*w y+30*h x+22*w y+31*h];
add_block('built-in/To Workspace', [sys '/ToWorkspace6'], 'Position', pos);
set_param('HabNet_v1/ToWorkspace6', 'VariableName','params_Pressure_Controlling_Tech_Choice1','SaveFormat','Structure','MaxDataPoints', 'inf');

pos = [x+23*w y+30*h x+29*w y+31*h];
add_block('built-in/To Workspace', [sys '/ToWorkspace7'], 'Position', pos);
set_param('HabNet_v1/ToWorkspace7', 'VariableName','params_Atmosphere_Providing_Tech_Choice1','SaveFormat','Structure','MaxDataPoints', 'inf');

pos = [x+30*w y+30*h x+36*w y+31*h];
add_block('built-in/To Workspace', [sys '/ToWorkspace8'], 'Position', pos);
set_param('HabNet_v1/ToWorkspace8', 'VariableName','params_Atmosphere_Providing_Tech_Choice2','SaveFormat','Structure','MaxDataPoints', 'inf');

pos = [x+37*w y+30*h x+43*w y+31*h];
add_block('built-in/To Workspace', [sys '/ToWorkspace9'], 'Position', pos);
set_param('HabNet_v1/ToWorkspace9', 'VariableName','params_CO2_Removing_Tech_Choice','SaveFormat','Structure','MaxDataPoints', 'inf');

pos = [x+44*w y+30*h x+50*w y+31*h];
add_block('built-in/To Workspace', [sys '/ToWorkspace10'], 'Position', pos);
set_param('HabNet_v1/ToWorkspace10', 'VariableName','params_Circulating_Air_Tech_Choice','SaveFormat','Structure','MaxDataPoints', 'inf');

pos = [x+51*w y+30*h x+57*w y+31*h];
add_block('built-in/To Workspace', [sys '/ToWorkspace11'], 'Position', pos);
set_param('HabNet_v1/ToWorkspace11', 'VariableName','params_Cooling_Condensing_Tech_Choice','SaveFormat','Structure','MaxDataPoints', 'inf');

%% Add lines

% In the system Pressure_Controlling and Technology Choices

add_line(sys, 'Initialize_Regulator/1', 'Sum1/1', 'autorouting', 'on');
add_line(sys, 'Sum1/1', 'Regulator/1', 'autorouting', 'on');
add_line(sys, 'Regulator/1', 'Pressure_Controlling/1', 'autorouting', 'on');
add_line(sys, 'PPRV/1', 'Pressure_Controlling/2', 'autorouting', 'on');
add_line(sys, 'Pressure_Controlling/1', 'Memory1/1', 'autorouting', 'on');
add_line(sys, 'Memory1/1', 'Sum1/2', 'autorouting', 'on');
add_line(sys, 'Initialize_Habitat_Vector_O2_Storing/1', 'Sum3/2', 'autorouting', 'on');
add_line(sys, 'AR/1', 'Pressure_Controlling/3', 'autorouting', 'on');

% In the system O2_Storing and O2_Tank plus O2_Tank initialization and
% Habitat_Vector initialization

add_line(sys, 'O2_Tank/1', 'Memory2/1', 'autorouting', 'on');
add_line(sys, 'Memory2/1', 'Sum2/1', 'autorouting', 'on');
add_line(sys, 'Initialize_O2_Tank/1', 'Sum2/2', 'autorouting', 'on');
add_line(sys, 'Sum2/1', 'O2_Storing/1', 'autorouting', 'on');
add_line(sys, 'Pressure_Controlling/3', 'Sum3/1', 'autorouting', 'on');
add_line(sys, 'Sum3/1', 'O2_Storing/2', 'autorouting', 'on');
add_line(sys, 'O2_Storing/1', 'O2_Tank/1', 'autorouting', 'on');
add_line(sys, 'Memory2/1', 'Pressure_Controlling/4', 'autorouting', 'on');
add_line(sys, 'Initialize_O2_Tank/1', 'Pressure_Controlling/5', 'autorouting', 'on');


% In the human and metabolizing system and human initialization

add_line(sys, 'Human/1', 'Metabolizing/1', 'autorouting', 'on');
add_line(sys, 'Metabolizing/1', 'Memory3/1', 'autorouting', 'on');
add_line(sys, 'Initialize_Human/1', 'Sum4/1', 'autorouting', 'on');
add_line(sys, 'Memory3/1', 'Sum4/2', 'autorouting', 'on');
add_line(sys, 'Sum4/1', 'Human/1', 'autorouting', 'on');

% In the AR initialization loop

add_line(sys, 'Initialize_AR/1', 'Sum5/2', 'autorouting', 'on');
add_line(sys, 'Memory4/1', 'Sum5/1', 'autorouting', 'on');
add_line(sys, 'Sum5/1', 'AR/1', 'autorouting', 'on');
add_line(sys, 'AR/1', 'O2_Storing/3', 'autorouting', 'on');
add_line(sys, 'AR/1', 'Metabolizing/2', 'autorouting', 'on');

% In the PGA initialization loop

add_line(sys, 'Sum6/1', 'PGA/1', 'autorouting', 'on');
add_line(sys, 'Memory5/1', 'Sum6/1', 'autorouting', 'on');
add_line(sys, 'Initialize_PGA/1', 'Sum6/2', 'autorouting', 'on');

% In the Atmosphere Providing system

add_line(sys, 'Pressure_Controlling/2', 'Atmosphere_Providing/3', 'autorouting', 'on');
add_line(sys, 'AR/1', 'Atmosphere_Providing/4', 'autorouting', 'on');
add_line(sys, 'Metabolizing/2', 'Atmosphere_Providing/5', 'autorouting', 'on');
add_line(sys, 'Metabolizing/3', 'Atmosphere_Providing/6', 'autorouting', 'on');
add_line(sys, 'PGA/1', 'Atmosphere_Providing/7', 'autorouting', 'on');
add_line(sys, 'Atmosphere_Providing/2', 'Memory4/1', 'autorouting', 'on');
add_line(sys, 'Atmosphere_Providing/3', 'Memory5/1', 'autorouting', 'on');

% In the CO2 Removing system

add_line(sys, 'Atmosphere_Providing/1', 'CO2_Removing/1', 'autorouting', 'on');
add_line(sys, 'Sum7/1', 'CO2_Removing/2', 'autorouting', 'on');
add_line(sys, 'Memory6/1', 'Sum7/1', 'autorouting', 'on');
add_line(sys, 'Initialize_CCC/1', 'Sum7/2', 'autorouting', 'on');
add_line(sys, 'CCC/1', 'Memory6/1', 'autorouting', 'on');
add_line(sys, 'CO2_Removing/2', 'CCC/1', 'autorouting', 'on');

% In the Circulating air system

add_line(sys, 'Fan/1', 'Circulating_Air/4', 'autorouting', 'on');
add_line(sys, 'AR/1', 'Circulating_Air/3', 'autorouting', 'on');
add_line(sys, 'CO2_Removing/1', 'Circulating_Air/2', 'autorouting', 'on');
add_line(sys, 'Atmosphere_Providing/1', 'Circulating_Air/1', 'autorouting', 'on');
add_line(sys, 'Circulating_Air/1', 'Memory7/1', 'autorouting', 'on');

% In the feedback from the circulating air function to the atmosphere providing function

add_line(sys, 'Memory7/1', 'Sum8/1', 'autorouting', 'on');
add_line(sys, 'Initialize_Habitat_Vector_Circulating_Air/1', 'Sum8/2', 'autorouting', 'on');
add_line(sys, 'Sum8/1', 'Atmosphere_Providing/1', 'autorouting', 'on');

% In the cooling condensing system

add_line(sys, 'Sublimator/1', 'Cooling_Condensing/2', 'autorouting', 'on');
add_line(sys, 'Circulating_Air/2', 'Cooling_Condensing/1', 'autorouting', 'on');

% In the feedback from the cooling condensing function to the atmosphere providing function

add_line(sys, 'Cooling_Condensing/1', 'Memory8/1', 'autorouting', 'on');
add_line(sys, 'Initialize_Habitat_Vector_Cooling_Condensing/1', 'Sum9/1', 'autorouting', 'on');
add_line(sys, 'Memory8/1', 'Sum9/2', 'autorouting', 'on');
add_line(sys, 'Sum9/1', 'Atmosphere_Providing/2', 'autorouting', 'on');

% For the to workspace blocks for results plotting

add_line(sys, 'Atmosphere_Providing/1', 'ToWorkspace1/1', 'autorouting', 'on');
add_line(sys, 'O2_Tank/1', 'ToWorkspace2/1', 'autorouting', 'on');
add_line(sys, 'CO2_Removing/2', 'ToWorkspace3/1', 'autorouting', 'on');

% For the to workspace blocks for mass, power and volume calculations
add_line(sys, 'O2_Tank/2', 'ToWorkspace4/1', 'autorouting', 'on');
add_line(sys, 'PPRV/2', 'ToWorkspace5/1', 'autorouting', 'on');
add_line(sys, 'Regulator/2', 'ToWorkspace6/1', 'autorouting', 'on');
add_line(sys, 'PGA/2', 'ToWorkspace7/1', 'autorouting', 'on');
add_line(sys, 'AR/2', 'ToWorkspace8/1', 'autorouting', 'on');
add_line(sys, 'CCC/2', 'ToWorkspace9/1', 'autorouting', 'on');
add_line(sys, 'Fan/2', 'ToWorkspace10/1', 'autorouting', 'on');
add_line(sys, 'Sublimator/2', 'ToWorkspace11/1', 'autorouting', 'on');

% set simulation parameters

set_param('HabNet_v1','Solver','FixedStepDiscrete', 'StopTime', '25200', 'FixedStep', '0.1');

% save model

save_system



